package com.sky.web.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @PostMapping
    public Result save(@RequestBody DishDTO dto){
        dishService.save(dto);
        return Result.success();
    }

    @GetMapping("page")
    public Result findByPage(DishPageDTO dto){
       PageResult  result = dishService.findByPage(dto);
        return Result.success(result);
    }

    @GetMapping("{id}")
    public Result findById( @PathVariable Long id){
       DishVO dishVO = dishService.findById(id);
        return Result.success(dishVO);
    }

    @PutMapping
    public Result update(@RequestBody DishDTO dto){
        dishService.update(dto);

        return Result.success();
    }


    @DeleteMapping
    public Result deleteByIds(@RequestParam("ids") List<Long> ids){
        dishService.deleteByIds(ids);
        return Result.success();
    }

    @PostMapping("status/{status}")
    public Result updateStatus(@PathVariable Integer status,@RequestParam Long id){
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishService.updateStatus(dish);
        return Result.success();
    }

    @GetMapping("list")
    public Result findList(Long categoryId,String name){
        DishPageDTO dto = DishPageDTO.builder()
                .categoryId(categoryId)
                .name(name)
                .build();
        List<DishVO> list = dishService.findList(dto);
        return Result.success(list);

    }
}
